<template>
  <div class="apply">
    <div class="tou">
      <el-button type="primary" @click="handleput('')">
        添加
        <i class="el-icon-plus el-icon--right"></i>
      </el-button>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="group_name" label="角色名称"></el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handle_put(scope.row)"
            >添加权限</el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleput(scope.row)"
            >编辑</el-button>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel( scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="添加权限" :visible.sync="dialogFormVisible" width="40%">
      <el-tree
        v-if="dialogFormVisible"
        :data="data"
        show-checkbox
        node-key="id"
        :default-checked-keys="arr1"
        :default-expand-all="arr"
        :props="defaultProps"
        @check="handleNodeClick2"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="Visible" width="30%" center>
      <el-form
        :model="ruleForm"
        :rules="rules1"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="group_name">
          <el-input v-model="ruleForm.group_name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="fenye">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalPage"
        :page-size="pageSize"
        @current-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
};
import {
  user_group_list,
  user_create_group,
  user_menu,
  user_group_menu,
  user_create_group_menu1,
  user_group_del,
} from "../axios/api";
export default {
  data() {
    return {
      group_id: "",
      arr: true,
      arr1: [],
      menu_id: [],
      id: "",
      data: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
              children: [
                {
                  id: 9,
                  label: "三级 1-1-1",
                },
                {
                  id: 10,
                  label: "三级 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1",
            },
            {
              id: 6,
              label: "二级 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1",
            },
            {
              id: 8,
              label: "二级 3-2",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      Visible: false,
      page: 0,
      totalPage: 0,
      currentPage: 1,
      pageSize: 0,
      formLabelWidth: "80px",
      ruleForm: {
        group_name: "",
        token: sessionStorage.getItem("Token1"),
      },
      dialogFormVisible: false,
      disabled: true,
      tableData: [],
      multipleSelection: [],
      rules1: {
        group_name: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
        ],
      },
    };
  },

  components: {},
  created() {
    this.user_group_list();
    this.user_menu();
  },
  methods: {
    handleNodeClick2(nodes, node) {
      // this.data.forEach((item) => {
      //   item.children.forEach((items) => {
      //     node.checkedKeys.forEach((itemss) => {
      //       if (itemss == items.id) {
      //         node.checkedKeys.push(item.id);
      //       }
      //     });
      //   });
      // });
      // this.menu_id = Array.from(new Set(node.checkedKeys));
      this.menu_id = node.checkedKeys;
    },
    quxiao() {
      // console.log(123);
      this.dialogFormVisible = false;
      this.arr1 = [];
    },
    user_menu() {
      let params = {
        token: sessionStorage.getItem("Token1"),
      };
      user_menu(params).then((res) => {
        console.log(res);
        this.data = res.data.data;
        // this.data.forEach((item) => {
        //   this.arr.push(item.id);
        // });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.id) {
            this.ruleForm.id = this.id;
          }
          let params = this.ruleForm;
          user_create_group(params).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                message: "成功",
                type: "success",
              });
              this.ruleForm.group_name = "";
              this.ruleForm.id = "";
              this.id = "";
              this.Visible = false;
              this.user_group_list();
            } else {
              this.$message({
                message: res.data.msg,
              });
              this.user_group_list();
              this.ruleForm.id = "";
              this.id = "";
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.Visible = false;
    },
    handleput(e) {
      console.log(e);
      if (e) {
        this.ruleForm.group_name = e.group_name;
        this.id = e.id + "";
      }

      this.Visible = true;
    },
    queding() {
      console.log(this.group_id);
      console.log(this.menu_id);
      let params = {
        token: sessionStorage.getItem("Token1"),
        group_id: this.group_id,
        menu_id: this.menu_id,
      };
      user_create_group_menu1(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.dialogFormVisible = false;
        }
      });
    },
    user_group_list() {
      let params = {
        token: sessionStorage.getItem("Token1"),
        // page: this.currentPage4,
      };
      user_group_list(params).then((res) => {
        console.log(res);
        this.currentPage = res.data.data.page + 0;
        this.totalPage = res.data.data.totalPage + 0;
        this.pageSize = parseInt(res.data.data.pageSize);
        this.tableData = res.data.data;
        this.tableData.forEach((item) => {
          var myDate = new Date(item.create_time * 1000);
          myDate.setDate(myDate.getDate());
          console.log(myDate.setDate(myDate.getDate()));
          item.create_time = myDate.format("yyyy年MM月dd日");
        });
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
    },

    handle_put(e) {
      console.log(e);
      this.group_id = e.id;
      let params = {
        group_id: e.id,
        token: sessionStorage.getItem("Token1"),
      };
      user_group_menu(params).then((res) => {
        // console.log(res);
        this.arr1 = res.data.data;
        console.log(this.arr1);
        this.dialogFormVisible = true;
      });
    },
    handleDel(e) {
      let params = {
        token: sessionStorage.getItem("Token1"),
        group_id: e.id,
      };
      user_group_del(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.user_group_list();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      console.log(val);
      if (val.length == this.tableData.length) {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
      this.multipleSelection = val;
    },
  },
};
</script>

<style scoped>
.apply {
  width: 100%;
  height: 100%;
  background-color: #fff;
  overflow: hidden;
}
.table {
  height: 80%;
  overflow-y: auto;
  box-sizing: border-box;
  margin: 0 20px;
}
.caozuo {
  box-sizing: border-box;
  padding: 20px;
  display: flex;
  justify-content: space-between;
}
.fenye {
  width: 100%;
  display: flex;
  justify-content: center;
}
.tou {
  margin: 10px 20px;
}
</style>